/* To find all cubes that match the given "vision string", use the following
 * algorithm:
 *
 * 0. Sort the side faces of each vision string in lexical order. Then sort
 *    the six vision strings in lexical order. We call this the "canonical
 *    vision string" of the cube. This is to simplify future comparison.
 * 1. Take the 6 colors from the 6 front faces of the given vision strings. 
 *    These 6 colors must form the actual faces of the cube. Next we want to
 *    find all permutations of these colors to form all possible cubes, but
 *    only leave the canonical ones (i.e. the lexical minimum after rotation)
 *    for consideration. To find these canonical cubes, we do the following.
 * 2. Fix the lexically smallest color as the front face (position 1).
 * 3. Find all permutations of the remaining 5 colors. There are no more than
 *    5! = 120 permutations (if the colors are all different). However, since
 *    the cube can be rotated, we only keep the cube if it is the lexical 
 *    minimum of its 24 rotations.
 * 4. For each canonical cube, we find its "canonical vision string", and 
 *    compare it to the input. If it matches for the first time, output it.
 *    If it matches for the second time, output it and terminate the program.
 * 5. If no canonical cube could match the input string, output "Impossible".
 *
 * Additional notes:
 * 
 * 1. A necessary condition for the problem to be solvable is the number of
 *    occurrence of each color in the input vision string must be a multiple
 *    of 5.
 * 2. There are a total of 6! = 720 permutations of the colors. However, for
 *    each cube, there are 24 permutations by rotation. Therefore there are 
 *    only 720/24=30 different ways to draw 1-6 on a cube.
 * 3. Another way to think about it is by constructing a cube in the following
 *    way:
 *    a. Choose the front face to be 1. Then there are 5 possibilities to 
 *       choose as the back face. Each choice yields a different set of cubes.
 *    b. Suppose we choose 6 as the back face. Then we are left with 2,3,4,5
 *       to use on the side faces. Since we can rotate the cube clockwise, we
 *       can without loss of generality assume the top side to be the smallest
 *       number, say 2. Then the rest three numbers can permute to form all
 *       combinations of the cube. That is, 3! = 6.
 *    c. Therefore the total number of different ways is 5 * 3! = 30.
 */

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <algorithm>

/* Base rotations of a cube with 1-6 on its faces. In each rotation, the
 * first number is on the front face, the last number is on the back face,
 * and the four numbers in the middle are on the side faces. All rotations
 * of a cube can be generated by rotating the four middle-numbers in each
 * of these base rotations.
 */
static const int base_rotation[6][6] = {
    { 1, 2, 3, 4, 5, 6 },
    { 2, 1, 3, 6, 5, 4 },
    { 3, 1, 4, 6, 2, 5 },
    { 4, 1, 3, 6, 5, 2 },
    { 5, 1, 4, 6, 2, 3 },
    { 6, 2, 3, 4, 5, 1 }
};

void build_vision_string(const char cube[6], char vision[6][6])
{
    int i, j;
    for (i = 0; i < 6; i++)
    {
        vision[i][0] = cube[i];
        for (j = 1; j <= 4; j++)
            vision[i][j] = cube[base_rotation[i][j]-1];
        vision[i][5] = '\0';
    }
}

void bubble_sort(char s[], int n)
{
    int i, j;
    for (i = 0; i < n; i++)
        for (j = i + 1; j < n; j++)
            if (s[i] > s[j])
            {
                char t = s[i];
                s[i] = s[j];
                s[j] = t;
            }
}

void get_canonical_vision_string(char vision[6][6])
{
    int i;
    for (i = 0; i < 6; i++)
        bubble_sort(vision[i] + 1, 4);
    qsort(vision, 6, 6, (int (*)(const void *, const void *))strcmp);
}

int is_minimum_of_rotations(const char cube[6])
{
    char rotated[7] = { 0 };
    int i, j;
    for (i = 0; i < 6; i++) /* try each base rotation i */
    {
        for (j = 0; j < 6; j++)
            rotated[j] = cube[base_rotation[i][j]-1];
        for (j = 0; j < 4; j++)
        {
            if (j > 0)
            {
                char t = rotated[1];
                rotated[1] = rotated[2];
                rotated[2] = rotated[3];
                rotated[3] = rotated[4];
                rotated[4] = t;
            }
            if (memcmp(cube, rotated, 6) > 0)
                return 0;
        }
    }
    return 1;
}

int solve()
{
    char vision[6][6];
    char cube[7] = { 0 };
    int i, match = 0;

    /* Input visions. */
    for (i = 0; i < 6; i++)
    {
        if (scanf("%s", vision[i]) != 1)
            return 0;
    }
    get_canonical_vision_string(vision);

    /* Find the faces of the actual cube. */
    for (i = 0; i < 6; i++)
        cube[i] = vision[i][0];

    /* Check each canonical permutation of the faces. */
    do
    {
        char v[6][6];

        /* Skip if the cube is not the minimum among its rotations. */
        if (!is_minimum_of_rotations(cube))
            continue;

        /* Build a vision string of this canonical cube. */
        build_vision_string(cube, v);

        /* Canonicalize the vision string. */
        get_canonical_vision_string(v);

        /* Compare to the input. */
        if (memcmp(vision, v, 36) == 0)
        {
            printf("%s\n", cube);
            if (++match == 2)
                return 1;
        }

    } while (std::next_permutation(cube+1, cube+6));

    if (match == 0)
        printf("Impossible\n");
    return 1;
}

int main()
{
#if 1
    while (solve());
#else
    char vision[6][6];
    int i;
    build_vision_string("PQRQPS", vision);
    for (i = 0; i < 6; i++)
        printf("%s ", vision[i]);
    printf("\n");
#endif
    return 0;
}
